package com.easyspringboot.bulider;

import com.easyspringboot.bean.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @Author xiaoyi
 * @Version 1.0
 * @ClassName BuildApplictionProperties
 * @Time 15:11
 */
public class BuildApplictionProperties {
    private static final Logger logger = LoggerFactory.getLogger(BuildController.class);

    public static void execute() {
        File folder = new File(Constants.PATH_RESOURCE);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File poFile = new File( folder,"application.properties");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(poFile);
            outw = new OutputStreamWriter(out, "gbk");
            bw = new BufferedWriter(outw);
            //      DB_DRIVER_NAME = PropertiesUtils.getProperties("db.driver.name");
            //        DB_URL = PropertiesUtils.getProperties("db.url");
            //        DB_USERNAME = PropertiesUtils.getProperties("db.username");
            //        DB_PASSWORD = PropertiesUtils.getProperties("db.password");
            bw.write("spring.datasource.driver-class-name =  " + Constants.DB_DRIVER_NAME );
            bw.newLine();
            bw.write("spring.datasource.url = " + Constants.DB_URL );
            bw.newLine();
            bw.write("spring.datasource.username = " + Constants.DB_USERNAME );
            bw.newLine();
            bw.write("spring.datasource.password = " + Constants.DB_PASSWORD );
            bw.newLine();
            bw.flush();

        } catch (Exception e) {
            logger.error("创建失败", e);
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }

}
